package com.softwinner.bt.util;

import java.util.ArrayList;


import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class ContactsQueryUtil {

	public static final String AUTOHORITY = "com.android.ecar.provider.contacts";
	private static final String TABLE_NAME="phonebook";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTOHORITY + "/"+TABLE_NAME);
	
	public static ArrayList<Bundle> queryContact(Context context ,String name,String phoneID){
		ArrayList<Bundle> result = new ArrayList<Bundle>();
		Cursor cursor  = null;
		
		try{
		ContentResolver cr = context.getContentResolver();

		 cursor =	cr.query(CONTENT_URI, null,null, new String [] {name,phoneID}, null);
		if(cursor != null){
//			result = new ArrayList<Bundle>();
			cursor.moveToFirst();
			while(!cursor.isAfterLast())
			{  String phoneNumer = cursor.getString(1);
			   String	phoneName = cursor.getString(0);
			   Bundle b = new Bundle();
			   String reg="[`~!@#$%^&*()+=|{}':;',//[//].<>/?~��@#��%����&*��������+|{}��������������������,����]";  
				phoneName = phoneName.replaceAll(reg, "").trim();		
				phoneName = phoneName.replaceAll(" ","");
				if(phoneName.contains(name)){
					b = new Bundle();
					b.putString("phoneNumber", phoneNumer);
					b.putString("name", phoneName);
					
					Log.i("ecar",b.toString());
					result.add(b);
				}
				cursor.moveToNext();
			}
		}
		}finally{
			if(cursor != null){
				cursor.close();
			}
		}
	 return result;		
	}    
}
